package com.fitness.front.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.front.convertor.CourseConvertor;
import com.fitness.front.modal.course.dto.CourseDto;
import com.fitness.front.modal.course.entity.Course;
import com.fitness.front.modal.course.params.CoursePageQueryParams;
import com.fitness.front.modal.course.vo.CourseVO;
import com.fitness.front.modal.response.ServerResponseEntity;
import com.fitness.front.service.ICourseService;
import com.fitness.front.service.impl.common.RecommenderServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/front/course")
public class CourseController {

    private final ICourseService courseService;
    private final CourseConvertor courseConvertor;
    private final RecommenderServiceImpl recommenderService;
    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<CourseVO>> page(CoursePageQueryParams queryParams) {
        Page<CourseVO> result = courseService.getCoursePage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 新增
     * @param equipmentDto
     * @return
     */
    @PostMapping("/add")
    public ServerResponseEntity<Boolean> add(@RequestBody CourseDto equipmentDto) {
        boolean result = courseService.addCourse(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10001","添加课程失败");
    }

    /**
     * 修改
     * @param equipmentDto
     * @return
     */
    @PutMapping("/update")
    public ServerResponseEntity<Boolean> update(@RequestBody CourseDto equipmentDto) {
        boolean result = courseService.updateCourse(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10002","更新课程信息失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ServerResponseEntity<Boolean> delete(@RequestBody Long id) {
        boolean result = courseService.removeById(id);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("C10003","删除课程信息失败");
    }

    /**
     * 获取课程表单
     */
    @GetMapping("/getCourseForm")
    public ServerResponseEntity<CourseVO> getCourseForm(Long id) {
        Course result = courseService.getById(id);
        return ServerResponseEntity.success(courseConvertor.entity2Vo(result));
    }

    /**
     * 获取推荐课程
     * @param userId
     * @param courseId
     * @return
     */
    @RequestMapping("/getRecommendCourse")
    public ServerResponseEntity<List<CourseVO>> getRecommendCourse(Long userId, Long courseId) {
        return ServerResponseEntity.success(recommenderService.recommend(userId, courseId));
    }
}
